package kr.hkit.powerjava.chapter14.windowbuilder;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Cards extends JPanel {
	CardLayout layout;
	public Cards() {
		layout = new CardLayout();
		setLayout(layout);
		for(int i = 1 ; i <= 10 ; i++) {
			add(new JButton("현재 카드번호 " + i + "입니다."));
		}	//버튼이 포개어져있음
	}
}

class CardFrame extends JFrame {	
	Cards cards;
	public CardFrame(String title) {
		initialize();
	}

	private void initialize() {
		setSize(400, 200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new JPanel(new GridLayout(0,5,10,0));
		
		addButton("<<", panel);
		addButton("<", panel);
		addButton(">>", panel);
		addButton(">", panel);
		addButton("종료", panel);
		
		add(panel, BorderLayout.NORTH);
		cards= new Cards();
		add(cards, BorderLayout.CENTER);
		
		setVisible(true);
	}

	private void addButton(String text, JPanel panel) {
		JButton btn = new JButton(text);
		btn.addActionListener(listener);
		panel.add(btn);
	}
	
	ActionListener listener = new ActionListener() { //필드로 접근
		public void actionPerformed(java.awt.event.ActionEvent e) {
/*			System.out.println("getActionCommend() : " + e.getActionCommand());
			System.out.println("getSource() : " + e.getSource());
			System.out.println("getID() : " + e.getID());*/
			if(e.getActionCommand().equals("종료")) {
				System.exit(0);
			} else if(e.getActionCommand().equals("<<")) {
				cards.layout.first(cards);
			} else if(e.getActionCommand().equals("<")) {
				cards.layout.previous(cards);
			} else if(e.getActionCommand().equals(">")) {
				cards.layout.next(cards);
			} else if(e.getActionCommand().equals(">>")) {
				cards.layout.last(cards);
			}
		}
	};
}

public class CardLayoutEx {
	public static void main(String[] args) {
		new CardFrame("CardLayout예제");
	}
}